<?php
namespace app\common\model;
use x\Xbase;
class User extends Xbase
{

    //微信二维码登录
    public function wxloginQrcode($uid = ''){
    // $data = ['img_url'=>'http://www.larryms.com/static/larrycms/images/779334991.png', 'order_no'=>'111'];
    // return $data;
      $app = m('Weixin','c')->getApp();
      $this->db->query("delete from pc_login where create_time<?",[time()-3600]);
      $order_no = $this->com_order_no();
      $data = [
        'status'=>0,
        'order_no'=>$order_no,
        'tj_user_id' => (int)$uid,
        'create_time' => time(),
      ];
      $this->db->insert('pc_login', $data);
      $result = $app->qrcode->temporary($order_no, 3600);
      $txt = urlencode($result['url']);
      $img_url = url('bbs/com/qrcode',['txt'=>$txt]);
      $data = ['img_url'=>$img_url, 'order_no'=>$order_no];
      return $data;
    }

    /**
     * 保存用户信息
     * [saveUser description]
     * @param  [type] $d [description]
     * @return [type]    [description]
     */
    public function saveUser($d){
        $data = [
            "username" => $d['username'],
            "name" => $d['name'],
            "sex" => $d['sex'],
            "update_time" => time()
        ];
        $userid = $d["user_id"];
        $result = $this->db->update("user",$data,["id=?",[$userid]]);
        //保存银行
        $userbank = $this->db->query("select * from user_bank where user_id=?",[$userid],1);
        $bankdata = [
            "user_id" => $userid,
            "user_name" => $d['name'],
            "bank_name" => $d['bankname'],
            "bank_wangdian" => $d['bankuser'],
            "bank_no" => $d['banknumber'],
            "update_time" => time()
        ];
        if(count($userbank) > 0){//已存在
            return $this->db->update("user_bank",$bankdata,["id=?",[$userbank['id']]]);
        }else{
            $bankdata["create_time"] = time();
            return $this->db->insert('user_bank',$bankdata);
        }
    }


    //是否登录
    public function isLogin(){
        $token = cookie('token','',false,[],'bbs');
        if($token){
            $sql = "select * from user where token=?";
            $rs = $this->db->query($sql,[$token],1);
            return $rs ? $rs : [];
        }
        return [];
    }

    /**
     * 登录
     * @param  [type] $username [description]
     * @param  [type] $password [description]
     */
  	public function login($username, $password){
        /* 获取用户数据 */
        $user = $this->db->query('select * from user where username=? or email=?',[$username,$username],1);
        if (empty($user) || (int)$user['status'] !== 0) {
            $this->setError('用户不存在或被禁用','username');
            return false;
        }
        if($user['password'] != com_password($password)){
            $this->setError('密码错误','password');
            return false;
        }
        return $this->recordLogin($user);
  	}

    /**
     * 记录用户登录
     * @param  [type] $user [用户信息]
     * @param  [type] $type [客户端]
     * @return [type]       [description]
     */
    public function recordLogin($user, $type=0){
        $token = token($user['id']);
        $ip = ip();
        $data = [
            'id' => $user['id'],
            'last_login_ip' => $ip,
            'last_login_time' => time(),
            'login_times' => $user['login_times']+1,
            'update_time' => time(),
        ];
        switch ($type) {
            case 0:
            default:
                $type = 0;
                $data['token'] = $token;
                break;
        }
        $this->db->insert('user_login',['user_id'=>$user['id'],'ip'=>$ip,'type'=>$type]);
        $this->db->update('user',$data);
        cookie('token',$token);
        return $token;
    }
}
